package debux.apps.mltr.model;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class MLTRDatabaseAdapter {
	// Database fields
	//table albums
	public static final String KEY_ALBUM_ID = "_id";
	public static final String KEY_ALBUM_NAME = "name";
	public static final String KEY_ALBUM_GENRE = "genre";
	public static final String KEY_ALBUM_RELEASED = "released";
	public static final String KEY_ALBUM_LABEL = "label";
	public static final String KEY_ALBUM_LINK = "link";
	public static final String DB_TABLE_ALBUMS = "albums";
	public static final String[] KEY_TABLE_ALBUMS = {KEY_ALBUM_ID,
													KEY_ALBUM_NAME,
													KEY_ALBUM_GENRE,
													KEY_ALBUM_RELEASED,
													KEY_ALBUM_LABEL,
													KEY_ALBUM_LINK};
	//table songs
	public static final String KEY_SONGS_ID = "_id";
	public static final String KEY_SONGS_NAME = "name";
	public static final String KEY_SONGS_GENRE = "genre";
	public static final String KEY_SONGS_ALBUM_ID = "album_id";
	public static final String KEY_SONGS_LENGTH = "length";
	public static final String KEY_SONGS_ORDER_ID = "order_id";
	public static final String KEY_SONGS_LINK = "link";
	public static final String KEY_SONGS_LYRICS = "lyrics";
	public static final String DB_TABLE_SONGS = "songs";
	public static final String[] KEY_TABLE_SONGS = {KEY_SONGS_ID,
													KEY_SONGS_ALBUM_ID,		
													KEY_SONGS_NAME,
													KEY_SONGS_GENRE,
													KEY_SONGS_LENGTH,
													KEY_SONGS_ORDER_ID,
													KEY_SONGS_LINK,
													KEY_SONGS_LYRICS};	

	// db helper
	private static String DATABASE_PATH = "/data/data/debux.apps.mltr.app/databases/";
	private static final String DATABASE_NAME = "mltr.db";
	public Context context;
	public SQLiteDatabase db;
	public MLTRDatabaseHelper dbHelper;
	
	public MLTRDatabaseAdapter(Context context){
		this.context = context;
	}
	public MLTRDatabaseAdapter open() throws SQLException{
		dbHelper= new MLTRDatabaseHelper(context);
		try{
			dbHelper.createDataBase();
			//db = SQLiteDatabase.openDatabase(DATABASE_PATH+DATABASE_NAME, null, SQLiteDatabase.OPEN_READONLY);
		} catch (Exception e){
			e.printStackTrace();
		}
		db = dbHelper.getWritableDatabase();

		return this;
	}
	
	public void close(){
		db.close();
	}
	
	public Cursor getAllAlbums(){
		return db.query(DB_TABLE_ALBUMS, KEY_TABLE_ALBUMS,null,null,null,null,null);
	}
	
	public Cursor getAllSongs(){
		return db.query(DB_TABLE_SONGS, KEY_TABLE_SONGS,null,null,"name",null,KEY_SONGS_NAME + " ASC");
	}
	
	public Cursor getAllSongsByAlbumId(int albumId){
		return db.query(DB_TABLE_SONGS, KEY_TABLE_SONGS, "album_id =?", new String[]{""+albumId}, null, null, KEY_SONGS_ORDER_ID + " ASC");
	}
	
	public Cursor searchAllSongsByName(String name){
		return db.query(DB_TABLE_SONGS, KEY_TABLE_SONGS, "name like ?", new String[]{"%"+name+"%"}, "name", null, KEY_SONGS_NAME + " ASC");
	}

}
